package com.example.smartcity.ui.Tools;



import android.content.Context;

import com.example.smartcity.ui.Tools.File;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {
    private static OkHttpClient okHttpClient;
    //get
    public static Call getCall(String url,String Token){
        okHttpClient = new OkHttpClient();
        Request request = new Request
                .Builder()
                .url(url)
                .addHeader("Authorization",Token)
                .build();
        Call call = okHttpClient.newCall(request);
        return call;
    }
    public static Call getCall(String url) {
        okHttpClient = new OkHttpClient();
        Request request = new Request
                .Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        return call;
    }

    //post
    public static Call getCall(String url, RequestBody body,String Token){
         okHttpClient = new OkHttpClient();
         Request request = new Request
                 .Builder()
                 .url(url)
                 .post(body)
                 .addHeader("Authorization",Token)
                 .build();
         Call call = okHttpClient.newCall(request);
         return call;
    }

    public static Call getCall(String url, RequestBody body){
        okHttpClient = new OkHttpClient();
        Request request = new Request
                .Builder()
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        return call;
    }
    //put
    public static Call getCall(String json,String url,String Token){
        okHttpClient = new OkHttpClient();
        MediaType Json = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(Json, json);
        Request request = new Request.Builder()
                .addHeader("Authorization",Token)
                .url(url)
                .put(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        return call;
    }



    public static String getBaseUrl(Context context){
        String baseUrl = "http://"+new File(context).readFile("IP");
        return baseUrl;
    }
}
